/*
 *
 * Copyright (C) 1997 Jayakrishnan Nair jk@csr.uvic.ca
 * WWW: http://www.csc.uvic.ca/~jk
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

import java.awt.*;
import java.applet.*;
import java.net.*;


public class ImageButton extends Canvas	{
  protected Panel panel;
  private Image image;
  private String text;
  private boolean mousePushed = false;
  private MediaTracker tracker;
  private int width;
  private int height;
  private int event;
  
  ImageButton( Image image, int width, int height, int event) {
    this.event = event;
    this.image = image;
    this.text = text;
    tracker = new MediaTracker(this);
    tracker.addImage(this.image,0);
    try {
      tracker.waitForID(0);
    } catch(InterruptedException e){}
    setBackground(Color.lightGray);
    this.width = width;
    this.height = height;
  }
  
  public boolean handleEvent(Event ev)    {
    switch (ev.id)      {
    case Event.MOUSE_DOWN:
      mousePushed = true;
      deliverEvent(new Event(this,event,""));
      repaint();
      return true;
    case Event.MOUSE_UP:
      if (mousePushed) {
	mousePushed = false;
	repaint();
      }
      
      return true;
    }
    return false;
  }
  
  public void update(Graphics g)    {
    paint(g);
  }
  
  public Dimension preferredSize() {
    return new Dimension(width,height);
  }

  public void paint(Graphics g)    {
    Dimension dim = size();
    int xSiz = image.getWidth(this);
    int ySiz = image.getHeight(this);
    int xPos = (dim.width - xSiz) / 2;
    int yPos = (dim.height - ySiz) / 2;
    g.setColor(Color.lightGray);
    g.fillRect(xPos, yPos, xSiz + 2, ySiz + 2);
    if(mousePushed)
      {
	g.setColor(Color.black);
	g.drawLine(0, 0, dim.width - 1, 0);
	g.drawLine(0, 0, 0, dim.height - 1);
	g.setColor(Color.gray);
	g.drawLine(1, 1, dim.width - 2, 1);
	g.drawLine(1, 1, 1, dim.height - 2);
	g.setColor(Color.lightGray);
	g.drawLine(1, dim.height - 2, dim.width - 2, dim.height - 2);
	g.drawLine(dim.width - 2, dim.height - 2, dim.width - 2, 1);
	g.setColor(Color.white);
	g.drawLine(0, dim.height - 1, dim.width - 1, dim.height - 1);
	g.drawLine(dim.width - 1, dim.height - 1, dim.width - 1, 0);
	g.clipRect(2, 2, dim.width - 4, dim.height - 4);
	g.drawImage(image, xPos + 2, yPos + 2, this);
      }    else			{
	g.setColor(Color.white);
	g.drawLine(0, 0, dim.width - 1, 0);
	g.drawLine(0, 0, 0, dim.height - 1);
	g.drawLine(1, 1, dim.width - 2, 1);
	g.drawLine(1, 1, 1, dim.height - 2);
	g.setColor(Color.black);
	g.drawLine(0, dim.height - 1, dim.width - 1, dim.height - 1);
	g.drawLine(dim.width - 1, dim.height - 1, dim.width - 1, 0);
	g.setColor(Color.gray);
	g.drawLine(1, dim.height - 2, dim.width - 2, dim.height - 2);
	g.drawLine(dim.width - 2, dim.height - 2, dim.width - 2, 1);
	g.clipRect(2, 2, dim.width - 4, dim.height - 4);
	g.drawImage(image, xPos, yPos, this);
      }
    
  }
}

